package cn.deschen.eshop.auth.validate.image;

import cn.deschen.eshop.auth.enums.ValidateCodeTypeEnum;
import cn.deschen.eshop.auth.properties.SecurityProperties;
import cn.deschen.eshop.auth.validate.AbstractValidateCodeProcessor;
import cn.deschen.eshop.auth.validate.ValidateCode;
import cn.deschen.eshop.auth.validate.ValidateCodeProcessor;
import cn.deschen.eshop.common.enums.GlobalEnum;
import cn.deschen.eshop.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import java.io.IOException;

/**
 * @Author hanbin_chen
 * @Description 图片验证码处理器
 * @DateTime 2021/1/14 13:41
 * @Version V1.0.0
 */
@Slf4j
@Component
public class ImageValidateCodeProcessor extends AbstractValidateCodeProcessor {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    protected void sendCode(ServletWebRequest request, ValidateCode code) {
        ImageValidateCode imageCode = (ImageValidateCode) code;
        try {
            ImageIO.write(imageCode.getImage(), "JPEG", request.getResponse().getOutputStream());
        } catch (IOException e) {
            log.error("【权限模块】发送验证码失败");
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "发送验证码失败");
        }
    }
}
